package control 
{
    import mx.rpc.soap.mxml.WebService;
    import mx.rpc.AbstractService;
    import mx.rpc.events.ResultEvent;
    import mx.controls.Alert;

    public class FlexService {

        private static var service:AbstractService;

        public static function getService():AbstractService {

            if (service == null) {
                var ws:Object = new WebService();
                ws.wsdl = "WebService.asmx?WSDL";
                ws.fetchWSDL();
                ws.useProxy = false;
                ws.showBusyCursor = true;
                ws.addEventListener("result",
                    function(event : ResultEvent) {
                        if (event.call.resultHandler)
                        	event.call.resultHandler(event); // Invoke the result handler specified when the asynchronous call was made
                    });
                ws.addEventListener("fault",
                    function(event:FaultEvent) {
                    	if (event.call.faultHandler)  // Invoke fault handler if provided
	                        event.call.faultHandler(event);
                    	else	
                        	Alert.show(event.fault.faultstring, "Error"); // Otherwise display a generic dialog
                    });
                service = ws;
                
            }
            return service;
        }
    }
}